#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<time.h>
#include "heap.c"
#include "merge_sort.c"
#include "quick_sort.c"
#include "insertion_sort_from_CLRS.c"
#include "merge.c"
#define MAX 50000	 //Quantity of random numbers to generate.
#define RANDOM rand() % 30001		//Specify the modulus for random numbers here.

void main()
{
	int i, j, rnd_num, file_size[6] = { 0 }, sorting_heap[MAX/6+1] = { 0 };
	char unsorted_file_name[6][15] = { {"rnd0"}, {"rnd1"}, {"rnd2"}, {"rnd3"}, {"rnd4"}, {"rnd5"} }, sorted_file_name[6][15] = { {"sorted0"}, {"sorted1"}, {"sorted2"}, {"sorted3"}, {"sorted4"}, {"sorted5"} }, merged_file_name[5][15] = { {"m01"}, {"m23"}, {"m45"}, {"m0123"}, {"m012345"} },	/*Arrays of i/p and o/p file names.*/	end[] = "--------END OF OPERATIONS---------";
	FILE *unsorted_file_pointer[6] = { 0 };		//Array of pointers to unsorted files.
	clock_t start, now;
	double elapsed_time;

    system("cls");

	start = clock ();		//Clock starts ticking.
	srand (time(NULL));		//Generate seed from current time.

	//Division of generated numbers into 6 files.
	for (i = 0; i <= 5; i++) {
		strcat (unsorted_file_name[i], ".txt");	   //Append ".txt" to each file name.
		strcat (sorted_file_name[i], ".txt");

		unsorted_file_pointer[i] = fopen (unsorted_file_name[i], "w");		//Numbers written are also readable. "w" will create a new file if one doesn't exist.

		for (j = 0; j < MAX/6; j++) { 	   //Loop places MAX/6 numbers in each file.
			rnd_num = RANDOM;
			fprintf (unsorted_file_pointer[i], "%d\n", rnd_num);
		}

		file_size[i] = MAX/6;		//Each file now contains MAX/6 numbers.
		fclose (unsorted_file_pointer[i]);
	}

	//If there are any numbers remaining (i.e. MAX%6 != 0), this loop places them one-by-one in files 0-4.
	for (j = MAX%6, i = 0; j > 0; j--, file_size[i]++, i++) {		//i indicates file number. j indicates quantity of remaining numbers. File size is incremented by 1 for each number placed.
		unsorted_file_pointer[i] = fopen (unsorted_file_name[i], "a");
		rnd_num = RANDOM;
		fprintf (unsorted_file_pointer[i], "%d\n", rnd_num);
		fclose (unsorted_file_pointer[i]);
	}

	now = clock ();
	elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\nFiles containing random numbers generated (%6.4fs).\n", elapsed_time);
	printf("\n----------SORTING FILES----------\n");

/////////HEAP SORT////////
	for (i = 0; i <= 5; i++) {
        start = clock ();
		heap_sort (sorting_heap, file_size[i], unsorted_file_name[i], sorted_file_name[i]);
		now = clock ();
		elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;		//Calculate the elapsed time since 'start'.
		printf("\tHeap sort has been performed on file %d (%6.4fs).\n", i, elapsed_time);
//		printf("\tFile IO time: %6.4fs.\n\n", file_io_time);     //file_io_time is a global variable defined in "array_and_file.c".
	}
	printf("\n\n");
/////////MERGE SORT////////
	for (i = 0; i <= 5; i++) {
        start = clock ();
		merge_sort (sorting_heap, file_size[i], unsorted_file_name[i], sorted_file_name[i]);
		now = clock ();
		elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;		//Calculate the elapsed time since 'start'.
		printf("\tMerge sort has been performed on file %d (%6.4fs).\n", i, elapsed_time);
	}
	printf("\n\n");
/////////QUICK SORT////////
	for (i = 0; i <= 5; i++) {
        start = clock ();
		quick_sort_main (sorting_heap, file_size[i], unsorted_file_name[i], sorted_file_name[i]);
		now = clock ();
		elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;		//Calculate the elapsed time since 'start'.
		printf("\tQuick sort has been performed on file %d (%6.4fs).\n", i, elapsed_time);
	}
	printf("\n\n");
/////////INSERTION SORT////////
	for (i = 0; i <= 5; i++) {
        start = clock ();
		InsertionSort (sorting_heap, file_size[i], unsorted_file_name[i], sorted_file_name[i]);
		now = clock ();
		elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;		//Calculate the elapsed time since 'start'.
		printf("\tInsertion sort has been performed on file %d (%6.4fs).\n", i, elapsed_time);
	}
	printf("\n\n");

	for (i = 0; i < 5; i++)
		strcat (merged_file_name[i], ".txt");


	printf("\n\n------MERGER OF SORTED FILES-----\n");
	for (i = 0; i <= 4; i += 2) {
        start = clock ();
		merge (sorted_file_name[i], file_size[i], sorted_file_name[i+1], file_size[i+1], merged_file_name[i/2]);
		now = clock ();
		elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;
		printf("\tFiles %d and %d have been merged (%6.4fs).\n", i, i+1, elapsed_time);
	}

    start = clock ();
	merge (merged_file_name[0], file_size[0] + file_size[1], merged_file_name[1], file_size[2] + file_size[3], merged_file_name[3]);
	now = clock ();
	elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tFiles %d, %d, %d and %d have been merged (%6.4fs).\n", 0, 1, 2, 3, elapsed_time);

	start = clock ();
	merge (merged_file_name[3], file_size[0] + file_size[1] + file_size[2] + file_size[3], merged_file_name[2], file_size[4] + file_size[5], merged_file_name[4]);
	now = clock ();
	elapsed_time = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tFiles %d, %d, %d, %d, %d and %d have been merged (%6.4fs).\n\n", 0, 1, 2, 3, 4, 5, elapsed_time);

	for (i = 0; i < strlen (end); i++) {
		printf("%c", end[i]);
        sleep(40);
	}

	fflush(stdin);
	getch();
}

